/*
 * @Author: szx
 * @Date: 2022-04-29 14:00:29
 * @LastEditTime: 2022-04-29 14:07:40
 * @Description:
 * @FilePath: \leetcode\剑指offerII\099\099.js
 */
/**
 * @param {number[][]} grid
 * @return {number}
 */
var minPathSum = function (grid) {
    const m = grid.length,
        n = grid[0].length;
    const dp = new Array(m).fill(0).map(() => new Array(n).fill(0));
    let tmp = 0;
    for (let i = 0; i < m; i++) {
        tmp += grid[i][0];
        dp[i][0] = tmp;
    }
    tmp = 0;
    for (let i = 0; i < n; i++) {
        tmp += grid[0][i];
        dp[0][i] = tmp;
    }
    for (let i = 1; i < m; i++) {
        for (let j = 1; j < n; j++) {
            dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
        }
    }
    return dp[m - 1][n - 1];
};
